﻿using System.Runtime.Intrinsics.X86;

namespace least_square_procedure
{
    internal class Program
    {
        /// <summary>
        /// 传入定义的函数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Calculate();
            Test01();
  
        }
        static double b = 0;
        static double a = 0;
        /// <summary>
        /// 根据公式计算出平均值、b、a
        /// </summary>
        static void Calculate()
        {
            int[] Year =
            {
                2020,
                2019,
                2018,
                2017,
                2016,
                2015,
                2014,
                2013,
                2012,
                2011,
                2010,
                2009,
                2008,
                2007,
                2006,
                2005,
                2004,
                2003,
                2002,
                2001,
                2000,
                1999,
                1998,
                1997,
                1996,
                1995,
                1994,
                1993,
                1992,
                1991,
                1990,
                1989,
                1988,
                1987,
                1986,
                1985,
                1984,
                1983,
                1982,
                1981,
                1980,
                1979,
                1978,
                1977,
                1976,
                1975,
                1974,
                1973,
                1972,
                1971,
                1970,
                1969,
                1968,
                1967,
                1966,
                1965,
                1964,
                1963,
                1962,
                1961,
                1960
            };
            double[] GDP =
            {
                14687673892882,
                14279937500608,
                13894817549380,
                12310409370894,
                11233276536744,
                11061553079871,
                10475682920597,
                9570406235659,
                8532229986993,
                7551500124197,
                6087163874510,
                5101703073088,
                4594307032667,
                3550342737009,
                2752131773358,
                2285965892364,
                1955347004965,
                1660287965663,
                1470550015077,
                1339395718862,
                1211346869600,
                1093997267277,
                1029043097558,
                961603952954,
                863746717507,
                734547898224,
                564324670008,
                444731282435,
                426915712715,
                383373318083,
                360857912565,
                347768051311,
                312353631207,
                272972974764,
                300758100107,
                309488028132,
                259946510957,
                230686747153,
                205089699858,
                195866382432,
                191149211575,
                178280594413,
                149540752829,
                174938098826,
                153940455341,
                163431551779,
                144182133387,
                138544284708,
                113687586299,
                99800958648,
                92602973434,
                79705906247,
                70846535055,
                72881631326,
                76720285969,
                70436266146,
                59708343488,
                50706799902,
                47209359005,
                50056868957,
                59716467625
            };
            //将所有的GDP数值转化为对数
            for (int i = 0; i < Year.Length; i++)
            {
                GDP[i] = Math.Log(GDP[i]);
            }
            double AverageX = Year.Average();
            double AverageY = GDP.Average();
            double NumeratorB = 0;
            double DenominatorB = 0;
            //实现公式中定义的求和计算
            for (int i = 0; i < Year.Length; i++)
            {
                double NumeratorB1 = (Year[i] - AverageX) * (GDP[i] - AverageY);
                NumeratorB += NumeratorB1;
                double DenominatorB1 = (Year[i] - AverageX) * (Year[i] - AverageX);
                DenominatorB += DenominatorB1;

            }
            b = NumeratorB / DenominatorB;
            a = AverageY - (b * AverageX);
            Console.WriteLine(a);
            Console.WriteLine(b);
        }
        /// <summary>
        /// 预测2021年的GDP
        /// </summary>
        static void Test01()
        {
            int input = 2021;
            double predict = (a + b * input);
            //将对数值转化为实数值
            predict = Math.Pow(Math.E, predict);
            Console.WriteLine($"{input}年的预测结果是：{predict}");
        }
    }
}